﻿-- =======================================================================
-- Author:		zengfei	
-- Create date: 09m/14/2011
-- Description:	更新采购订单明细表(此系统中用做入货单，所以进行库存更新)
-- =======================================================================
CREATE PROCEDURE [dbo].[Usp_ModifyPurchaseDetail]
	@p_Action	char(1),
	@P_Warehouse	int,
	@p_PurchaseNo	varchar(30),
	@p_LineNo	varchar(3),
	@p_ProductNo	varchar(10),
	@p_LotNo	varchar(20), 
	@p_ProductDate	datetime, 
	@p_Qty	numeric(10,2), 
	@p_Price	numeric(10,2),
	@p_Amount	numeric(15,2),
	@p_DiscountRate	numeric(10,4),
	@p_DiscountPrice	numeric(10,2),
	@p_DiscountAmt	numeric(15,2),
	@p_TaxRate	numeric(10,4),
	@p_TaxAmount	numeric(15,2),
	@p_PriceInTax	numeric(10,2),
	@p_AmountInTax	numeric(15,2),
	@p_ValidDate	datetime, 
	@p_Remark	varchar(60),
	@p_RecordID	int out,
	@p_UserID	varchar(60),
	@p_ErrMsg	varchar(100) out,
	@p_RFlag	smallint out
AS
declare	@CurQty	numeric(15, 2),
		@TmpQty	numeric(15, 2),
		@CBalQty	numeric(15, 2)

select @CurQty = Qty from Balance where ProductNo = @p_ProductNo and Warehouse = @p_Warehouse

begin transaction

if	@p_Action = 'A'
begin
	select @TmpQty = @p_Qty, @CBalQty = @p_Qty
	
	insert into PurchaseDetail (PurchaseNo, [LineNo], ProductNo, LotNo, ProductDate, Qty, Price, Amount, DiscountRate, DiscountPrice, DiscountAmt, TaxRate, TaxAmount, PriceInTax, AmountInTax, ValidDate, Remark)
		values (@p_PurchaseNo, @p_LineNo, @p_ProductNo, @p_LotNo, @p_ProductDate, @p_Qty, @p_Price, @p_Amount, @p_DiscountRate, @p_DiscountPrice, @p_DiscountAmt, @p_TaxRate, @p_TaxAmount, @p_PriceInTax, @p_AmountInTax, @p_ValidDate, @p_Remark)
		
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
	
	select @p_RecordID = @@IDENTITY
end
else if	@p_Action = 'C'
begin
	select @TmpQty = @p_Qty - Qty from PurchaseDetail where RecordId = @p_RecordId
	
	if	@TmpQty < 0 and @CurQty < -@TmpQty
	begin
		raiserror('当前商品[%s]库存不足', 16, 1, @p_ProductNo)
		goto ErrHandle
	end
	
	update PurchaseDetail set [LineNo] = @p_LineNo,
							ProductNo = @p_ProductNo, 
							LotNo = @p_LotNo, 
							ProductDate = @p_ProductDate, 
							Qty = @p_Qty, 
							Price = @p_Price, 
							Amount = @p_Amount, 
							DiscountRate = @p_DiscountRate,
							DiscountPrice = @p_DiscountPrice, 
							DiscountAmt = @p_DiscountAmt, 
							TaxRate = @p_TaxRate, 
							TaxAmount = @p_TaxAmount, 
							PriceInTax = @p_PriceInTax, 
							AmountInTax = @p_AmountInTax, 
							ValidDate = @p_ValidDate, 
							Remark = @p_Remark 
						where RecordID = @p_RecordID 
							
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
	
	select @CBalQty = @TmpQty
end
else if	@p_Action = 'D'
begin
	select @TmpQty = -Qty from PurchaseDetail where RecordId = @p_RecordId
	
	if	@TmpQty < 0 and @CurQty < -@TmpQty
	begin
		raiserror('当前商品[%s]库存不足', 16, 1, @p_ProductNo)
		goto ErrHandle
	end
	
	delete from PurchaseDetail where RecordID = @p_RecordID 
	
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
	
	select @CBalQty = @TmpQty
end

insert into PurchaseDetail_log (PurchaseNo, [LineNo], ProductNo, LotNo, ProductDate, Qty, Price, Amount, DiscountRate, DiscountPrice, DiscountAmt, TaxRate, TaxAmount, PriceInTax, AmountInTax, ValidDate, Remark, RecordID, UserID, ModifyTime, [Action])
	values (@p_PurchaseNo, @p_LineNo, @p_ProductNo, @p_LotNo, @p_ProductDate, @p_Qty, @p_Price, @p_Amount, @p_DiscountRate, @p_DiscountPrice, @p_DiscountAmt, @p_TaxRate, @p_TaxAmount, @p_PriceInTax, @p_AmountInTax, @p_ValidDate, @p_Remark, @p_RecordID, @p_UserID, GETDATE(), @p_Action)
	
if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle

exec Usp_UpdateBalance @p_ProductNo, @p_Warehouse, @CBalQty, 0

commit transaction

set @p_RFlag = 1


ErrHandle:

if	@@TRANCOUNT > 0	rollback transaction

set @p_RFlag = -1

